<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<title>JPIPKAK - JPIP Streaming</title>
</head>
<body bgcolor="#ffffff">
  <h1>JPIPKAK - JPIP Streaming</h1>
  <p>
  JPEG 2000 Interactive Protocol (JPIP) flexibility with respect to random access, code stream reordering and incremental decoding is highly exploitable in a networked environment allowing access to remote large files using limited bandwidth connections or high contention networks.
  </p>
  <h2>JPIPKAK - JPIP Overview</h2>
  <p>
  A brief overview of the JPIP event sequence is presented in this section, more information can be found at <a target="_blank" href="http://www.jpeg.org/jpeg2000/j2kpart9.html">JPEG 2000 Interactive Protocol (Part 9 – JPIP)</a> and the specification can (and should) be purchased from <a target="_blank" href="http://www.iso.org">ISO</a>.
  </p>
  <p>
  An earlier version of JPEG 2000 Part 9 is available here <a href="http://www.jpeg.org/public/fcd15444-9v2.pdf">http://www.jpeg.org/public/fcd15444-9v2.pdf</a>, noting the ISO copyright, diagrams are not replicated in this documentation.
  </p>
  <p>
  The JPIP protocol has been abstracted in this format driver, requests are made at the 1:1 resolution level.
  </p>
  <img src="jpipsequence.png" alt="JPIP Sequence Diagram"/>
  <ol>
    <li><a name="initJPIP">Initial</a> JPIP request for a target image, a target id, a session over http, data to be returned as a jpp-stream are requested and a maximum length is put on the response.  In this case no initial window is requested, though it can be.
Server responds with a target identifier that can be used to identify the image on the server and a JPIP-cnew response header which includes the path to the JPIP server which will handle all future requests and a cid session identifier.  A session is required so that that the server can model the state of the client connection, only sending the data that is required.
</li>
   <li>Client requests particular view windows on the target image with a maximum response length and includes the session identifier established in the previous communication.
'fsiz' is used to identify the resolution associated with the requested view-window. The values 'fx' and 'fy' specify the dimensions of the desired image resolution.
'roff' is used to identify the upper left hand corner off the spatial region associated with the requested view-windw.
'rsiz' is used to identify the horizontal and vertical extents of the spatial region associated with the requested view-window.</li>
  </ol>
  <h2>JPIPKAK -approach</h2>
  <p>
  The JPIPKAK driver uses an approach that was first demonstrated here, <a target="_blank" href="http://www.drc-dev.ohiolink.edu/browser/J2KViewer">J2KViewer</a>, by Juan Pablo Garcia Ortiz of separating the communication layer (socket / http) from the Kakadu kdu_cache object.  Separating the communication layer from the data object is desirable since it allows the use of optimized http client libraries such as libcurl, Apache HttpClient (note that  jportiz used a plain Java socket) and allows SSL communication between the client and server.
  </p>
  <p>
Kakadu's implementation of client communication with a JPIP server uses a socket, and this socket connection holds the state for this client session.  A client session with Kakadu can be recreated using the JPIP cache operations between client and server, but no use of traditional HTTP cookies is supported since JPIP is neutral to the transport layer.
 </p>
 <p>
The JPIPKAK driver is written using a HTTP client library with the Kakadu cache object and supports optimized communication with a JPIP server (which may or may not support HTTP sessions) and the high performance of the kakadu kdu_region_decompressor.
  </p>
  <img src="components.PNG" alt="Component Diagram"/>
  <h2>JPIPKAK - implementation</h2>
  <p>
  The implementation supports the GDAL C++ and C API, and provides an initial SWIG wrapper for this driver with a Java ImageIO example (<b>TODO</b> - qGIS Example).
  </p>
  <p>
  The driver uses a simple threading model to support requesting reads of the data and remote fetching.  This threading model supports two separate client windows, with just one connection to the server.  Requests to the server are multiplexed to utilize available bandwidth efficiently. The client identifies these windows by using “0” (low) or “1” (high) values to a “PRIORITY” metadata request option.
  </p>
  <i>
  <p>
  Note: SSL support
  </p>
  <p>If the client is built with support for SSL, then driver determines whether to use SSL if the request is a jpips:// protocol as opposed to jpip:// . Note that the driver does not verify server certificates using the Curl certificate bundle and is currently set to accept all SSL server certificates.
</p>
   <p>
   Note: libCurl
   </p>
   <p>
   JPIP sets client/server values using HTTP headers, modifications have been made to the GDAL  HTTP portability library to support this.
   </p>
  </i>
  <img src="gdalsequence.PNG" alt="GDAL Sequence Diagram"/>
  <ol>
    <li>
    GDALGetDatasetDriver
    <p>
    Fetch the driver to which this dataset relates.
    </p>
    </li>
    <li>
    Open
    <p>
    If the filename contained in the <code>GDALOpenInfo</code> object
    has a case insensitive URI scheme of JPIP or JPIPS
    the <code>JPIPKAKDataset</code> is created and initialized</a>,
    otherwise NULL is returned.
    </p>
    </li>
    <li>
    Initialize
    <p>
    Initialization involves making an initial connection to the JPIP
    Server to establish a session and to retrieve the initial metadata
    about the image (ref. <a href="#initJPIP">JPIP Sequence
    Diagram</a>).
    </p>
    <p>
    If the connection fails, the function returns false and the <code>Open</code> function returns NULL indicating that opening the dataset with this driver failed.
    </p>
    <p>
    If the connection is successful, then subsequent requests to the JPIP server are made to retrieve all the available metadata about the image.  Metadata items are set using the <code>GDALMajorObject->SetMetadataItem</code> in the "JPIP" domain.
    </p>
    <p>
    If the metadata returned from the server includes GeoJP2 UUID box, or a GMLJP2 XML box then this metadata is parsed and sets the geographic metadata of this dataset.
    </p>
    </li>
    <li>
    GDALGetMetadata
    <p>
    C API to <code>JPIPKAKDataset->GetMetadata</code>
    </p>
    </li>
    <li>
    GetMetadata
    <p>
    returns metadata for the "JPIP" domain, keys are "JPIP_NQUALITYLAYERS", "JPIP_NRESOLUTIONLEVELS", "JPIP_NCOMPS" and "JPIP_SPRECISION"
    </p>
    </li>
    <li>
    GDALEndAsyncRasterIO
    <p>
    If the asynchronous raster IO is active and not required, the C API calls <code>JPIPKAKDataset->EndAsyncRasterIO</code>
    </p>
    </li>
    <li>
    EndAsyncRasterIO
    <p>
    The JPIPKAKAsyncRasterIO object is deleted
    </p>
    </li>
    <li>
    delete
    <p>
    </p>
    </li>
    <li>
    GDALBeginAsyncRasterIO
    <p>
    C API to <code>JPIPKAKDataset->BeginAsyncRasterIO</code>
    </p>
    </li>
    <li>
    BeginAsyncRasterIO
    <p>
    The client has set the requested view window at 1:1 and have optionally set the discard level, quality layers and thread priority metadata items.
    </p>
    </li>
    <li>
    Create
    <p>
    Creates a JPIPKAKAsyncRasterIO Object
    </p>
    </li>
    <li>
    Start
    <p>
    Configures the kakadu machinery and starts a background thread (if not already running) to communicate to the server the current view window request.  The background thread results in the <code>kdu_cache</code> object being updated until the JPIP server sends an "End Of Response" (EOR) message for the current view window request.
    </p>
    </li>
    <li>
    GDALLockBuffer<a name="GDALLockBuffer"/>
    <p>
    C API to LockBuffer
    </p>
    </li>
    <li>
    LockBuffer<a name="LockBuffer"/>
    <p>
    Not implemented in <code>JPIPKAKAsyncRasterIO</code>, a lock is acquired in <code>JPIPKAKAsyncRasterIO->GetNextUpdatedRegion</code>
    </p>
    </li>
    <li>
    GDALGetNextUpdatedRegion<a name="GDALGetNextUpdatedRegion"/>
    <p>
    C API to GetNextUpdatedRegion
    </p>
    </li>
    <li>
    GetNextUpdatedRegion<a name="GetNextUpdatedRegion"/>
    <p>
    The function decompresses the available data to generate an image (according to the dataset buffer type set in <code>JPIPKAKDataset->BeginAsyncRasterIO</code>)
    The window width, height (at the requested discard level) decompressed is returned in the region pointer and can be rendered by the client.
    The status of the rendering operation is one of <code>GARIO_PENDING, GARIO_UPDATE, GARIO_ERROR, GARIO_COMPLETE</code> from the <code>GDALAsyncStatusType</code> structure.  <code>GARIO_UPDATE, GARIO_PENDING</code> require more reads of GetNextUpdatedRegion to get the full image data, this is the progressive rendering of JPIP.   <code>GARIO_COMPLETE</code> indicates the window is complete.
    </p>
    <p>
    <code>GDALAsyncStatusType</code> is a structure used by<code>GetNextUpdatedRegion</code> to indicate whether the function should be called again when either kakadu has more data in its cache to decompress, or the server has not sent an End Of Response (EOR) message to indicate the request window is complete.
    </p>
    <p>
    The region passed into this function is passed by reference, and the caller can read this region when the result returns to find the region that has been decompressed.  The image data is packed into the buffer, e.g. RGB if the region requested has 3 components.
    </p>
    </li>
    <li>
    GDALUnlockBuffer<a name="GDALUnlockBuffer"/>
    <p>
    C Api to UnlockBuffer
    </p>
    </li>
    <li>
    UnlockBuffer<a name="UnlockBuffer"/>
    <p>
    Not implemented in <code>JPIPKAKAsyncRasterIO</code>, a lock is acquired in <code>JPIPKAKAsyncRasterIO->GetNextUpdatedRegion</code>
    </p>
    </li>
    <li>
    Draw<a name="Draw"/>
    <p>
    Client renders image data
    </p>
    </li>
    <li>
    <a href="#GDALLockBuffer">GDALLockBuffer</a>
    <p>
    </p>
    </li>
    <li>
    <a href="#LockBuffer">LockBuffer</a>
    <p>
    </p>
    </li>
    <li>
    <a href="#GDALGetNextUpdatedRegion">GDALGetNextUpdatedRegion</a>
    <p>
    </p>
    </li>
    <li>
    <a href="#GetNextUpdatedRegion">GetNextUpdatedRegion</a>
    <p>
    </p>
    </li>
    <li>
    <a href="#GDALUnlockBuffer">GDALUnlockBuffer</a>
    <p>
    </p>
    </li>
    <li>
    <a href="#UnlockBuffer">UnlockBuffer</a>
    <p>
    </p>
    </li>
    <li>
    <a href="#Draw">Draw</a>
    <p>
    </p>
    </li>
  </ol>
  <h2>JPIPKAK - installation requirements</h2>
  <ul>
    <li><a target="_blank" href="http://curl.haxx.se/">Libcurl 7.9.4</a></li>
    <li><a target="_blank" href="http://www.openssl.org/">OpenSSL 0.9.8K</a> (if SSL is required, a JPIPS connection)</li>
    <li><a target="_blank" href="http://www.kakadusoftware.com">Kakadu</a> (tested with v5.2.6 and v6)</li>
  </ul>
  <p>Currently only a Windows makefile is provided, however this should compile on Linux as well as there are no Windows dependencies.</p>
  <p>See Also:
    <ul>
        <li><a target="_blank" href="http://www.jpeg.org/jpeg2000/j2kpart9.html">JPEG 2000 Interactive Protocol (Part 9 – JPIP)</a></li>
        <li><a target="_blank" href="http://www.opengeospatial.org/standards/gmljp2">http://www.opengeospatial.org/standards/gmljp2</a></li>
        <li><a target="_blank" href="http://www.kakadusoftware.com">Kakadu Software </a></li>
        <li><a target="_blank" href="http://iasdemo.ittvis.com/">IAS demo (example JPIP(S) streams)</a></li>
    </ul>
  </p>
  <h2>NOTES</h2>
  <p>
  Driver originally developed by <a target="_blank" href="http://www.ittvis.com">ITT VIS</a> and donated to GDAL to enable SSL enabled JPIP client streaming of remote JPEG 2000 datasets.
  </p>
</body>
</html>
